package javapop.framework.parser.tuple;

import java.util.List;

import javapop.framework.ParseContext;
import javapop.framework.ParseError;
import javapop.framework.ParseInput;
import javapop.framework.ParseNull;
import javapop.framework.ParseResult;
import javapop.framework.Parser;
import javapop.framework.parser.TupleParser;

public class Zero extends Parser<Object> {
	private TupleParser parser;
	
	public Zero() {
		parser = new TupleParser();
	}
		
	public Zero then(Parser<?> silent) {
		parser.addSilent(silent);
		return this;
	}
	
	public Zero fatalFrom(int ref) {
		parser.setFatalFrom(ref);
		return this;
	}
	
		
	@Override
	public ParseResult<Object> eval(ParseContext ctx, ParseInput input) {
		ParseResult<List<?>> result = parser.parse(ctx, input);
		if(result.isError()) {
			return (ParseResult) result.asError();
		}
		List<?> results = result.getResult();
		if(results.size()!=0) {
			return (ParseResult) new ParseError("Wrong cardinality: expected 0 got "+results.size());
		}
		
		return buildResult(new ParseNull());
	}

	@Override
	public String getDescription() {
		return parser.getDescription();
	}
	
	@Override
	public boolean isMemoized() {
		return false;
	}

}
